﻿using System.Linq;
using Ewk.MachineLearning.Classification.Interfaces;
using Ewk.MachineLearning.Persistance.DataAccess;
using Ewk.MachineLearning.Persistance.Domain;

namespace Ewk.MachineLearning.Classification.Conversions.EntityVectorFactories
{
    /// <summary>
    /// Creates a vector representation of a <see cref="Conversation"/>.
    /// </summary>
    class ConversationVectorFactory : IEntityVectorFactory
    {
        #region Implementation of IEntityFactory

        /// <summary>
        /// Creates a vector representation of the <see cref="Conversation"/>.
        /// </summary>
        /// <returns>A vector representation of the <see cref="Conversation"/>.</returns>
        public EntityVector<double>[] CreateEntityVectors()
        {
            using (var catalog = CatalogFactory.NewCatalog())
            {
                var conversationRepository = new EntityRepository<Conversation>(catalog);
                var conversations = conversationRepository.Entities
                                                          .Where(c =>
                                                                 c.Messages.Count > 0);

                var wordRepository = new TextEntityRepository<Word>(catalog);
                var words = wordRepository.Entities
                                               .Cast<IEntity>()
                                               .ToList();

                return
                    conversations
                        .Select(conversation =>
                                new EntityVector<double>(
                                    conversation.Id.ToString(),
                                    conversation.Messages.ToVector<Message, Word>(words)))
                        .ToArray();
            }
        }

        #endregion
    }
}